<!DOCTYPE html>
<!-- saved from url=(0041)http://c.biancheng.net/view/vip_7075.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" async="" charset="utf-8" src="./10.7Java finally和return的执行顺序（非常重要）_files/core.php"></script><script async="" src="./10.7Java finally和return的执行顺序（非常重要）_files/click.php"></script>

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
<!-- 启用Chromium高速渲染模式 -->
<meta name="renderer" content="webkit">
<meta name="force-rendering" content="webkit">
<!-- 禁止百度转码 -->
<meta name="applicable-device" content="pc,mobile">
<meta name="MobileOptimized" content="width">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<!-- 禁止识别电话号码 -->
<meta name="format-detection" content="telephone=no">

<link rel="shortcut icon" href="http://c.biancheng.net/favicon.ico?v=1.6.72">
<link href="./10.7Java finally和return的执行顺序（非常重要）_files/common.css" rel="stylesheet">
<title>Java finally和return的执行顺序（非常重要）</title>
<meta name="description" content="在 Java 的异常处理中，try、catch 和 finally 是按顺序执行的。如果 try 中没有异常，则顺序为 tryfinally，如果 try 中有异常，则顺序为 trycatchfinally。但是当 try、catch、finally 中加入 return 之后">
</head>
<body>
<div id="topbar" class="clearfix">
	<ul id="product-type" class="left">
		<li>
			<a href="http://c.biancheng.net/"><span class="iconfont iconfont-home"></span>首页</a>
		</li>
		<li class="active">
			<a href="http://c.biancheng.net/sitemap/" rel="nofollow"><span class="iconfont iconfont-book"></span>教程</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/vip/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-vip"></span>VIP会员</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/q2a/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-q2a"></span>一对一答疑</a>
		</li>
		<li>
			<a href="http://fudao.biancheng.net/" rel="nofollow" target="_blank"><span class="iconfont iconfont-fudao"></span>辅导班</a>
		</li>
	</ul>
</div>
<div id="header" class="clearfix">
	<a id="logo" class="left" href="http://c.biancheng.net/">
		<img height="26" src="./10.7Java finally和return的执行顺序（非常重要）_files/logo.png" alt="C语言中文网">
	</a>
	<ul id="nav-main" class="hover-none left clearfix">
		<li class="wap-yes"><a href="http://c.biancheng.net/">首页</a></li>
		<li><a href="http://c.biancheng.net/c/">C语言教程</a></li>
		<li><a href="http://c.biancheng.net/cplus/">C++教程</a></li>
		<li><a href="http://c.biancheng.net/python/">Python教程</a></li>
		<li><a href="http://c.biancheng.net/java/">Java教程</a></li>
		<li><a href="http://c.biancheng.net/linux_tutorial/">Linux入门</a></li>
		<li><a href="http://c.biancheng.net/sitemap/" title="网站地图">更多&gt;&gt;</a></li>
	</ul>
	<span id="sidebar-toggle" class="toggle-btn" toggle-target="#sidebar">目录 <span class="glyphicon"></span></span>

	<a href="http://vip.biancheng.net/?from=topbar" class="user-info glyphicon glyphicon-user hover-none" target="_blank" rel="nofollow" title="用户中心"></a>
</div>
<div id="main" class="clearfix">
	<div id="sidebar" class="toggle-target">
	<div id="contents">
		<dt><span class="glyphicon glyphicon-option-vertical" aria-hidden="true"></span><a href="http://c.biancheng.net/java/">Java教程</a></dt>
		
			<dd>
				<span class="channel-num">1</span>
				<a href="http://c.biancheng.net/java/10/">Java入门基础及环境搭建</a>
			</dd>
		
			<dd>
				<span class="channel-num">2</span>
				<a href="http://c.biancheng.net/java/20/">Java程序设计基础</a>
			</dd>
		
			<dd>
				<span class="channel-num">3</span>
				<a href="http://c.biancheng.net/java/30/">Java流程控制语句</a>
			</dd>
		
			<dd>
				<span class="channel-num">4</span>
				<a href="http://c.biancheng.net/java/40/">Java字符串处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">5</span>
				<a href="http://c.biancheng.net/java/50/">Java数字和日期处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">6</span>
				<a href="http://c.biancheng.net/java/60/">Java内置包装类</a>
			</dd>
		
			<dd>
				<span class="channel-num">7</span>
				<a href="http://c.biancheng.net/java/70/">Java数组处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">8</span>
				<a href="http://c.biancheng.net/java/80/">Java类和对象</a>
			</dd>
		
			<dd>
				<span class="channel-num">9</span>
				<a href="http://c.biancheng.net/java/90/">Java继承和多态</a>
			</dd>
		<dd class="this"> <span class="channel-num">10</span> <a href="http://c.biancheng.net/java/100/">Java异常处理</a> </dd><dl class="dl-sub"><dd>10.1 <a href="http://c.biancheng.net/view/6635.html">Java异常处理</a></dd><dd>10.2 <a href="http://c.biancheng.net/view/6637.html">Java中Error和Exception的异同</a></dd><dd>10.3 <a href="http://c.biancheng.net/view/1043.html">Java异常处理机制</a></dd><dd>10.4 <a href="http://c.biancheng.net/view/6732.html">Java try catch语句详解</a></dd><dd>10.5 <a href="http://c.biancheng.net/view/vip_7074.html">【Java项目实战】计算平均成绩</a><span class="glyphicon glyphicon-usd"></span></dd><dd>10.6 <a href="http://c.biancheng.net/view/1046.html">Java try catch finally语句</a></dd><dd class="active">10.7 <a href="http://c.biancheng.net/view/vip_7075.html">Java finally和return的执行顺序（非常重要）</a><span class="glyphicon glyphicon-usd"></span></dd><dd>10.8 <a href="http://c.biancheng.net/view/6745.html">Java 9增强的自动资源管理</a></dd><dd>10.9 <a href="http://c.biancheng.net/view/6751.html">Java声明和抛出异常</a></dd><dd>10.10 <a href="http://c.biancheng.net/view/6734.html">Java 7新特性之多异常捕获</a></dd><dd>10.11 <a href="http://c.biancheng.net/view/1051.html">Java自定义异常</a></dd><dd>10.12 <a href="http://c.biancheng.net/view/1052.html">Java验证用户信息</a></dd><dd>10.13 <a href="http://c.biancheng.net/view/vip_7076.html">【Java示例】完善除法运算的错误信息</a><span class="glyphicon glyphicon-usd"></span></dd><dd>10.14 <a href="http://c.biancheng.net/view/vip_7077.html">Java异常处理规则（新手必看）</a><span class="glyphicon glyphicon-usd"></span></dd><dd>10.15 <a href="http://c.biancheng.net/view/6657.html">Java的异常跟踪栈</a></dd><dd>10.16 <a href="http://c.biancheng.net/view/6689.html">Java.util.logging记录日志</a></dd></dl>
			<dd>
				<span class="channel-num">11</span>
				<a href="http://c.biancheng.net/java/110/">Java集合、泛型和枚举</a>
			</dd>
		
			<dd>
				<span class="channel-num">12</span>
				<a href="http://c.biancheng.net/java/120/">Java反射机制</a>
			</dd>
		
			<dd>
				<span class="channel-num">13</span>
				<a href="http://c.biancheng.net/java/130/">Java输入/输出流</a>
			</dd>
		
			<dd>
				<span class="channel-num">14</span>
				<a href="http://c.biancheng.net/java/java/135/">Java注解</a>
			</dd>
		
	</div>
</div>
	<div id="article-wrap">
		<div id="article">
			<div class="arc-info">
	<span class="position"><span class="glyphicon glyphicon-map-marker"></span> <a href="http://c.biancheng.net/">首页</a> &gt; <a href="http://c.biancheng.net/java/">Java教程</a> &gt; <a href="http://c.biancheng.net/java/100/">Java异常处理</a></span>
	<span class="read-num">阅读：4,850</span>
</div>

<div id="ad-position-bottom"></div>
			<h1>Java finally和return的执行顺序（非常重要）</h1>
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/1046.html">Java try catch finally语句</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/6745.html">Java 9增强的自动资源管理</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-top"><p class="pic"></p><p class="text" adid="java-1v1q2a"><a href="http://c.biancheng.net/view/7552.html" target="_blank" style="color: #D33428;">C语言中文网推出辅导班啦，包括<span style="color: #008000;">「C语言辅导班、C++辅导班、算法/数据结构辅导班」</span>，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线，随时响应！</a></p></div>
			<div id="arc-body">在 Java 的异常处理中，try、catch 和 finally 是按顺序执行的。如果 try 中没有异常，则顺序为 try→finally，如果 try 中有异常，则顺序为 try→catch→finally。但是当 try、catch、finally 中加入 return 之后，return 和 finally 的执行顺序让很多人混淆不清。下面来分别说明一下。<br>
<h4>
	1. try 和 catch 中带有&nbsp;return</h4>
1）try 中带有 return
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display: none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7075.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7075.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7075.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">tryDemo</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">int</span> <span class="sh_function">show</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_keyword">return</span> <span class="sh_number">1</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span></li><li>            System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"执行finally模块"</span><span class="sh_symbol">);</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span><span class="sh_usertype">String</span><span class="sh_normal"> </span>args<span class="sh_symbol">[])</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_function">show</span><span class="sh_symbol">());</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class tryDemo {
    public static int show() {
        try {
            return 1;
        } finally {
            System.out.println("执行finally模块");
        }
    }

    public static void main(String args[]) {
        System.out.println(show());
    }
}</pre></div></div>
输出结果如下：
<p class="info-box">
	执行finally模块<br>
	1</p>
2）try 和 catch 中都带有 return&nbsp;
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7075.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7075.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7075.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">tryDemo</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">int</span> <span class="sh_function">show</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_type">int</span> a <span class="sh_symbol">=</span> <span class="sh_number">8</span> <span class="sh_symbol">/</span> <span class="sh_number">0</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">return</span> <span class="sh_number">1</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">catch</span> <span class="sh_symbol">(</span><span class="sh_usertype">Exception</span><span class="sh_normal"> </span>e<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_keyword">return</span> <span class="sh_number">2</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span></li><li>            System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"执行finally模块"</span><span class="sh_symbol">);</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span><span class="sh_usertype">String</span><span class="sh_normal"> </span>args<span class="sh_symbol">[])</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_function">show</span><span class="sh_symbol">());</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class tryDemo {
    public static int show() {
        try {
            int a = 8 / 0;
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            System.out.println("执行finally模块");
        }
    }

    public static void main(String args[]) {
        System.out.println(show());
    }
}</pre></div></div>
输出结果为：
<p class="info-box">
	执行finally模块<br>
	2</p>
当 try 代码块或者 catch 代码块中有 return 时，finally 中的代码总会被执行，且 finally 语句 return 返回之前执行。<br>
<br>
注意：可以使用编译器的 Debug 功能查看详细过程。如果不了解如何使用 Debug 功能可参考《<a href="http://c.biancheng.net/view/1311.html" target="_blank">Java Eclipse如何调试代码</a>》一节。
<h4>
	2. finally 中带有 return</h4>
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7075.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7075.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7075.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">tryDemo</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">int</span> <span class="sh_function">show</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_type">int</span> a <span class="sh_symbol">=</span> <span class="sh_number">8</span> <span class="sh_symbol">/</span> <span class="sh_number">0</span><span class="sh_symbol">;</span></li><li>            <span class="sh_keyword">return</span> <span class="sh_number">1</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">catch</span> <span class="sh_symbol">(</span><span class="sh_usertype">Exception</span><span class="sh_normal"> </span>e<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_keyword">return</span> <span class="sh_number">2</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span></li><li>            System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"执行finally模块"</span><span class="sh_symbol">);</span></li><li>            <span class="sh_keyword">return</span> <span class="sh_number">0</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span><span class="sh_usertype">String</span><span class="sh_normal"> </span>args<span class="sh_symbol">[])</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_function">show</span><span class="sh_symbol">());</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class tryDemo {
    public static int show() {
        try {
            int a = 8 / 0;
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            System.out.println("执行finally模块");
            return 0;
        }
    }

    public static void main(String args[]) {
        System.out.println(show());
    }
}</pre></div></div>
输出结果如下：
<p class="info-box">
	执行finally模块<br>
	0</p>
当 finally 有返回值时，会直接返回该值，不会去返回 try 代码块或者 catch 代码块中的返回值。<br>
<br>
注意：finally 代码块中最好不要包含 return 语句，否则程序会提前退出。<br>
<h4>
	3. finally 中改变返回值</h4>
下面先来看 try 代码块或者 catch 代码块中的返回值是普通变量时，代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7075.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7075.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7075.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">tryDemo</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">int</span> <span class="sh_function">show</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_type">int</span> result <span class="sh_symbol">=</span> <span class="sh_number">0</span><span class="sh_symbol">;</span></li><li>        <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_keyword">return</span> result<span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span></li><li>            System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"执行finally模块"</span><span class="sh_symbol">);</span></li><li>            result <span class="sh_symbol">=</span> <span class="sh_number">1</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span><span class="sh_usertype">String</span><span class="sh_normal"> </span>args<span class="sh_symbol">[])</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_function">show</span><span class="sh_symbol">());</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class tryDemo {
    public static int show() {
        int result = 0;
        try {
            return result;
        } finally {
            System.out.println("执行finally模块");
            result = 1;
        }
    }

    public static void main(String args[]) {
        System.out.println(show());
    }
}</pre></div></div>
输出结果为：
<p class="info-box">
	执行finally模块<br>
	0</p>
由输出结果可以看出，在 finally 代码块中改变返回值并不会改变最后返回的内容。<br>
<br>
当返回值类型是引用类型时，结果也是一样的，代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7075.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7075.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7075.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">tryDemo</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_usertype">Object</span><span class="sh_normal"> </span><span class="sh_function">show</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_usertype">Object</span><span class="sh_normal"> </span>obj <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Object</span><span class="sh_symbol">();</span></li><li>        <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_keyword">return</span> obj<span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span></li><li>            System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"执行finally模块"</span><span class="sh_symbol">);</span></li><li>            obj <span class="sh_symbol">=</span> <span class="sh_keyword">null</span><span class="sh_symbol">;</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span><span class="sh_usertype">String</span><span class="sh_normal"> </span>args<span class="sh_symbol">[])</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_function">show</span><span class="sh_symbol">());</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class tryDemo {
    public static Object show() {
        Object obj = new Object();
        try {
            return obj;
        } finally {
            System.out.println("执行finally模块");
            obj = null;
        }
    }

    public static void main(String args[]) {
        System.out.println(show());
    }
}</pre></div></div>
输出结果为：
<p class="info-box">
	执行finally模块<br>
	java.lang.Object@15db9742</p>
当 try 代码块或 catch 代码块中的 return 返回值类型为普通变量或引用变量时，即使在后面 finally 代码块中对返回值的变量重新赋值，也不会影响最后返回的值。<br>
<br>
总结为以下几条：
<ul>
	<li>
		当 try 代码块和 catch 代码块中有 return 语句时，finally 仍然会被执行。</li>
	<li>
		执行 try 代码块或&nbsp;catch 代码块中的 return 语句之前，都会先执行 finally 语句。</li>
	<li>
		无论在 finally 代码块中是否修改返回值，返回值都不会改变，仍然是执行 finally 代码块之前的值。</li>
	<li>
		finally 代码块中的&nbsp;return 语句一定会执行。</li>
</ul>
</div>
			<div id="arc-append">
	<p>关注微信公众号「站长严长生」，在手机上阅读所有教程，随时随地都能学习。本公众号由<a class="col-link" href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">C语言中文网站长</a>运营，每日更新，坚持原创，敢说真话，凡事有态度。</p>
	<p style="margin-top:12px; text-align:center;">
		<img width="180" src="./10.7Java finally和return的执行顺序（非常重要）_files/qrcode_weixueyuan_original.png" alt="魏雪原二维码"><br>
		<span class="col-green">微信扫描二维码关注公众号</span>
	</p>
</div>
<!-- <p>关注微信公众号「魏雪原」，跟着<a class="col-link" href="/view/8092.html" target="_blank">站长</a>一起学习，拒绝躺平，跳出内卷，用知识完成阶级跃升：</p>
	<ul>
		<li>每日更新，坚持原创，敢说真话，凡事有态度。</li>
		<li>编程+求职+创业+营销，既有硬知识，也有软技能。</li>
		<li>手机端阅读教程，随时随地都能学习。</li>
		<li>即将绑定网站部分功能，包括注册登录、找回密码、获取下载链接等。</li>
	</ul> -->
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/1046.html">Java try catch finally语句</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/6745.html">Java 9增强的自动资源管理</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-bottom"></div>

<!-- <div id="ad-bottom-weixin" class="clearfix">
	<div class="left" style="width: 535px;">
		<p><span class="col-red">编程帮</span>，一个分享编程知识的公众号。跟着<a class="col-link" href="/cpp/about/author/" target="_blank">站长</a>一起学习，每天都有进步。</p>
		<p>通俗易懂，深入浅出，一篇文章只讲一个知识点。</p>
		<p>文章不深奥，不需要钻研，在公交、在地铁、在厕所都可以阅读，随时随地涨姿势。</p>
		<p>文章不涉及代码，不烧脑细胞，人人都可以学习。</p>
		<p>当你决定关注「编程帮」，你已然超越了90%的程序员！</p>
	</div>
	<div class="right" style="width: 150px;">
		<img width="150" src="/templets/new/images/erweima_biancheng.gif?v=1.6.72" alt="编程帮二维码" /><br />
		<span class="col-green">微信扫描二维码关注</span>
	</div>
</div> -->

<div id="nice-arcs" class="box-bottom">
	<h4>优秀文章</h4>
	<ul class="clearfix">
<li><a href="http://c.biancheng.net/view/1004.html" title="Java抽象（abstract）类">Java抽象（abstract）类</a></li>
<li><a href="http://c.biancheng.net/view/1249.html" title="多级反馈队列调度算法详解">多级反馈队列调度算法详解</a></li>
<li><a href="http://c.biancheng.net/view/1324.html" title="里氏替换原则——面向对象设计原则">里氏替换原则——面向对象设计原则</a></li>
<li><a href="http://c.biancheng.net/view/2171.html" title="Python变量的定义和使用">Python变量的定义和使用</a></li>
<li><a href="http://c.biancheng.net/view/2301.html" title="C++ typeid运算符：获取类型信息">C++ typeid运算符：获取类型信息</a></li>
<li><a href="http://c.biancheng.net/view/2307.html" title="Python异常处理机制到底有什么用？">Python异常处理机制到底有什么用？</a></li>
<li><a href="http://c.biancheng.net/view/2678.html" title="Unity 3D C#脚本基础">Unity 3D C#脚本基础</a></li>
<li><a href="http://c.biancheng.net/view/4378.html" title="MyBatis动态sql之trim、where、set标签">MyBatis动态sql之trim、where、set标签</a></li>
<li><a href="http://c.biancheng.net/view/7455.html" title="Django安装与配置教程（图解）">Django安装与配置教程（图解）</a></li>
<li><a href="http://c.biancheng.net/redis/incrby.html" title="Redis INCR数值操作命令">Redis INCR数值操作命令</a></li>
</ul>
</div>
		</div>
		
	</div>
</div>
<script type="text/javascript">
// 当前文章ID
window.arcIdRaw = 'a_' + 7075;
window.arcId = "9179dExLy/OjLUhW7k1bi7Z7iuehh19APJJbLw0LeQD1dUf3CACp9avcayE";
window.typeidChain = "101|79";
</script>
<div id="footer" class="clearfix">
	<div class="info left">
	<p>精美而实用的网站，分享优质编程教程，帮助有志青年。千锤百炼，只为大作；精益求精，处处斟酌；这种教程，看一眼就倾心。</p>
	<p>
		<a href="http://c.biancheng.net/view/8066.html" target="_blank" rel="nofollow">关于网站</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">关于站长</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8097.html" target="_blank" rel="nofollow">如何完成一部教程</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8093.html" target="_blank" rel="nofollow">联系我们</a> <span>|</span>
		<a href="http://c.biancheng.net/sitemap/" target="_blank" rel="nofollow">网站地图</a>
	</p>
	<p>Copyright ©2012-2021 biancheng.net, <a href="http://www.beian.miit.gov.cn/" target="_blank" rel="nofollow" style="color:#666;">陕ICP备15000209号</a></p>
	</div>
	<img class="right" src="./10.7Java finally和return的执行顺序（非常重要）_files/logo_bottom.gif" alt="底部Logo">
	<span id="return-top"><b>↑</b></span>
</div>

<script type="text/javascript">
window.siteId = 4;
window.cmsTemplets = "/templets/new";
window.cmsTempletsVer = "1.6.72";
</script>

<script src="./10.7Java finally和return的执行顺序（非常重要）_files/jquery1.12.4.min.js.下载"></script>
<script src="./10.7Java finally和return的执行顺序（非常重要）_files/common.js.下载"></script>
<span style="display:none;"><script src="./10.7Java finally和return的执行顺序（非常重要）_files/z_stat.php" type="text/javascript" defer="defer" async="async"></script></span>

</body></html>